package edu.pku.sei.pgie.persistence.xml.template;

import edu.pku.sei.pgie.analyzer.core.SymbolTable;
import edu.pku.sei.pgie.analyzer.util.*;

public class XMLRelationSerializer
{
  protected static String nl;
  public static synchronized XMLRelationSerializer create(String lineSeparator)
  {
    nl = lineSeparator;
    XMLRelationSerializer result = new XMLRelationSerializer();
    nl = null;
    return result;
  }

  protected final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "<relation type=\"";
  protected final String TEXT_2 = "\">" + NL + "\t<start-point refid=\"";
  protected final String TEXT_3 = "\">" + NL + "\t\t<![CDATA[";
  protected final String TEXT_4 = "]]>" + NL + "\t</start-point>" + NL + "\t<end-point refid=\"";
  protected final String TEXT_5 = "\">" + NL + "\t\t<![CDATA[";
  protected final String TEXT_6 = "]]>" + NL + "\t</end-point>" + NL + "</relation>";

  public String generate(Object argument)
  {
    StringBuffer stringBuffer = new StringBuffer();

	Object[] args = (Object[])argument;
	String[] relations = (String[]) args[0];
	SymbolTable symbolTable = (SymbolTable) args[1];
	String type = (String) args[2];
	String id0 = null;
	String id1 = null;
	if(symbolTable != null)
		id0 = symbolTable.resolveRefId(relations[0]);
		id1 = symbolTable.resolveRefId(relations[1]);

    stringBuffer.append(TEXT_1);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(id0);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(relations[0]);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(id1);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(relations[1]);
    stringBuffer.append(TEXT_6);
    return stringBuffer.toString();
  }
}
